// pages/commitOrder/commitOrder.js
const app = getApp();
import httpsUtils from '../../utils/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    shopId: '',
    plNum: [],
    commentLevel: '',
    isImg: '',
    plComment: [],
    selectIndex: '1',
    current: 1,
    hasNextPage: true,
    hpRate: 100
  },
  /**
   * 获取评论数
   */
  findCommentNumber: function () {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/comment/selectCount?goodsId=' + that.data.goodsId + '&shopId=' + that.data.shopId,
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        var rate = 100;
        if (json.data.total != 0) {
          rate = parseInt((json.data.hpSize / json.data.total) * 100);
        }
        that.setData({
          plNum: json.data,
          hpRate: rate
        })
      }
    })
  },
  /**
   * 获取评论内容
   */
  findComment: function () {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/comment/getCommentByCommentLevel?goodsId=' + that.data.goodsId + '&shopId=' + that.data.shopId + '&isImg=' + that.data.isImg + '&commentLevel=' + that.data.commentLevel + "&current=" + that.data.current,
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          plComment: that.data.plComment.concat(json.data.list),
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
        })
      }
    })
  },
  /**
   * 获取评论 通过条件
   */
  findCommentBy: function (e) {
    var that = this;
    var commentLevel = '';
    var isImg = '';
    var index = e.currentTarget.dataset.index;
    if (index == 1) {
      isImg = '';
      commentLevel = '';
    }
    if (index == 2) {
      isImg = '';
      commentLevel = 1;
    }
    if (index == 3) {
      isImg = '';
      commentLevel = 2;
    }
    if (index == 4) {
      isImg = '';
      commentLevel = 3;
    }
    if (index == 5) {
      isImg = '1';
      commentLevel = '';
    }
    that.setData({
      isImg: isImg,
      commentLevel: commentLevel,
      selectIndex: index,
      plComment:[]
    })
    that.findComment();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      goodsId: options.goodsId,
      shopId: options.shopId,
    })

    //查询评论数
    that.findCommentNumber();

    //查看评论
    that.findComment();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage) {
      that.setData({
        current: that.data.current + 1,
      })
      that.selectShopList(1);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})